/**
 * 日期格式化工具函数
 */

/**
 * 格式化日期为 YYYY-MM-DD HH:mm:ss 格式
 * @param {string|Date} date - 日期字符串或Date对象
 * @param {boolean} showTime - 是否显示时间，默认true
 * @returns {string} 格式化后的日期字符串
 */
export function formatDate(date, showTime = true) {
  if (!date) return 'N/A'
  
  const d = new Date(date)
  if (isNaN(d.getTime())) return 'N/A'
  
  const year = d.getFullYear()
  const month = String(d.getMonth() + 1).padStart(2, '0')
  const day = String(d.getDate()).padStart(2, '0')
  
  if (!showTime) {
    return `${year}-${month}-${day}`
  }
  
  const hours = String(d.getHours()).padStart(2, '0')
  const minutes = String(d.getMinutes()).padStart(2, '0')
  const seconds = String(d.getSeconds()).padStart(2, '0')
  
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

/**
 * 格式化日期为相对时间（如：刚刚、5分钟前、1小时前等）
 * @param {string|Date} date - 日期字符串或Date对象
 * @returns {string} 相对时间字符串
 */
export function formatRelativeTime(date) {
  if (!date) return 'N/A'
  
  const d = new Date(date)
  if (isNaN(d.getTime())) return 'N/A'
  
  const now = new Date()
  const diff = now.getTime() - d.getTime()
  
  const minute = 60 * 1000
  const hour = 60 * minute
  const day = 24 * hour
  const month = 30 * day
  const year = 365 * day
  
  if (diff < minute) {
    return '刚刚'
  } else if (diff < hour) {
    return `${Math.floor(diff / minute)}分钟前`
  } else if (diff < day) {
    return `${Math.floor(diff / hour)}小时前`
  } else if (diff < month) {
    return `${Math.floor(diff / day)}天前`
  } else if (diff < year) {
    return `${Math.floor(diff / month)}个月前`
  } else {
    return `${Math.floor(diff / year)}年前`
  }
}

/**
 * 获取日期范围（今天、昨天、本周、本月等）
 * @param {string} range - 范围类型：today, yesterday, week, month
 * @returns {object} 包含startDate和endDate的对象
 */
export function getDateRange(range) {
  const now = new Date()
  const today = new Date(now.getFullYear(), now.getMonth(), now.getDate())
  
  switch (range) {
    case 'today':
      return {
        startDate: formatDate(today, false),
        endDate: formatDate(today, false)
      }
    case 'yesterday':
      const yesterday = new Date(today.getTime() - 24 * 60 * 60 * 1000)
      return {
        startDate: formatDate(yesterday, false),
        endDate: formatDate(yesterday, false)
      }
    case 'week':
      const weekStart = new Date(today.getTime() - today.getDay() * 24 * 60 * 60 * 1000)
      return {
        startDate: formatDate(weekStart, false),
        endDate: formatDate(today, false)
      }
    case 'month':
      const monthStart = new Date(now.getFullYear(), now.getMonth(), 1)
      return {
        startDate: formatDate(monthStart, false),
        endDate: formatDate(today, false)
      }
    default:
      return {
        startDate: '',
        endDate: ''
      }
  }
}

/**
 * 验证日期格式是否正确
 * @param {string} dateString - 日期字符串
 * @param {string} format - 日期格式，默认 'YYYY-MM-DD'
 * @returns {boolean} 是否为有效日期
 */
export function isValidDate(dateString, format = 'YYYY-MM-DD') {
  if (!dateString) return false
  
  const date = new Date(dateString)
  return !isNaN(date.getTime())
}

/**
 * 比较两个日期
 * @param {string|Date} date1 - 第一个日期
 * @param {string|Date} date2 - 第二个日期
 * @returns {number} -1: date1 < date2, 0: date1 = date2, 1: date1 > date2
 */
export function compareDate(date1, date2) {
  const d1 = new Date(date1)
  const d2 = new Date(date2)
  
  if (d1.getTime() < d2.getTime()) return -1
  if (d1.getTime() > d2.getTime()) return 1
  return 0
} 